﻿
	using System;
	using System.Collections.Generic;
	using System.IO;
	using FulcrumApi.clients;
	using FulcrumApi.entities;
	using FulcrumApi.helpers;
	using Newtonsoft.Json;

namespace FulcrumApi
{
	public class FulcrumClient
	{

	FulcrumHttpClient client;

	public FulcrumClient(String fulcrumUrl) {
		this.client = new FulcrumHttpClient(fulcrumUrl);
	}
	
	public void authenticate(String userName, String password) {
		Dictionary<String, String> parameters = new Dictionary<String, String>();
        parameters.Add("utf8", "\u2713");
		parameters.Add("user[email]", userName);
		parameters.Add("user[password]", password);
		parameters.Add("user[remember_me]", "0");
		parameters.Add("commit", "Sign in");
		Stream res = client.doPost("/users/sign_in", parameters);
		String authOk = FulcrumParser.readAsString(res);
        if (!authOk.Contains("<html><body>You are being <a href=\"http://localhost:3000/\">redirected</a>.</body></html>"))
        {
			throw new AuthenticationfailedException();
		}
	}
	
	
	
    //public List<FulcrumUser> listUsers(Project project) {
    //    Stream json = client.doGet(String.Format("/projects/{0}/users.json", project.Id), new Dictionary<String, String>());
    //    return (List<FulcrumUser>)JsonDeserializer.Deserialize(new JsonReader(json), typeof(List<FulcrumUser>));
    //}

	public void createUser(Project project, String email, String name, String initials) {
        Dictionary<String, String> map = new Dictionary<String, String>();
		map.Add("user[email]", email);
		map.Add("user[name]", name);
		map.Add("user[initials]", initials);
		map.Add("commit", "Add user");
		client.doPost(String.Format("/projects/{0}/users", project.Id), map);
	}

	public void deleteUser(Project project, FulcrumUser user) {
        Dictionary<String, String> map = new Dictionary<String, String>();
		map.Add("_method", "delete");
		client.doPost(String.Format("/projects/{0}/users/{1}", project.Id, user.user.id), map);
	}

	public void updateUser(Project project, FulcrumUser user, String locale,
            String newPassword, bool emailDelivery, bool emailAcceptance, bool emailRejection,
			String currentPassword) {
                Dictionary<String, String> map = new Dictionary<String, String>();
//		map.Add("_method", "put");
		map.Add("user[email]", user.user.email);
        map.Add("user[name]", user.user.name);
		map.Add("user[initials]", user.user.initials);
        map.Add("user[locale]", user.user.initials);
        map.Add("user[password]", user.user.initials);
        map.Add("user[password_confirmation]", user.user.initials);
        map.Add("user[email_delivery]", user.user.initials);
        map.Add("user[email_acceptance]", user.user.initials);
        map.Add("user[email_rejection]", user.user.initials);
        map.Add("user[current_password]", user.user.initials);
		map.Add("commit", "Update");
		client.doPost(String.Format("/projects/{0}/users", project.Id), map);
	}


    
    }

}

